package org.loy.eclipse.post_debug_perspective.preferences;

import org.eclipse.jface.preference.*;
import org.eclipse.ui.IWorkbenchPreferencePage;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.*;

import org.loy.eclipse.post_debug_perspective.Activator;

public class PreferencePage extends FieldEditorPreferencePage implements IWorkbenchPreferencePage
{
	public PreferencePage()
	{
		super(GRID);
		setPreferenceStore(Activator.getDefault().getPreferenceStore());
		setDescription("These settings control how to switch perspective aftrer debug session.");
	}

	@Override
	public void init(IWorkbench workbench)
	{
		this.workbench = workbench;
	}
	
	@Override
	public void createFieldEditors()
	{
		addField( new BooleanFieldEditor( PreferenceConstants.P_IS_ENABLED,	"Switch perspective after debug session", 
			getFieldEditorParent()) );

		
		addField( new ComboFieldEditor(PreferenceConstants.P_PERSPECTIVE_TO_SET, "Perspective to set",
			getComboboxPerspectiveChoices(workbench), getFieldEditorParent()) );
	}

	public static String[][] getComboboxPerspectiveChoices(IWorkbench workbench)
	{
		IPerspectiveDescriptor[] perspectives = workbench.getPerspectiveRegistry().getPerspectives();
		
		String[][] entryNamesAndValues = new String[perspectives.length + 1][];

		String[] noneEntry = {"None", PreferenceConstants.DO_NOT_SWITCH_PERSPECTIVE};
		entryNamesAndValues[0] = noneEntry;

		for(int i = 0; i < perspectives.length; i++) {
			String[] entry = {perspectives[i].getLabel(), perspectives[i].getId()};
			entryNamesAndValues[i + 1] = entry;
		}

		return entryNamesAndValues;
	}

	private IWorkbench workbench;
}